package com.dingding.order.core.common.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.exception.bussiness.BizExpEnum;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.core.common.IUserOrderDao;

/**
 * @Description:用户订单dao
 * @author fengshuonan
 * @date 2016年7月6日 下午6:04:24
 * @version 叮叮搭车
 * @since 2.3.0
 */
@Component("userOrderCommonDao")
public class UserOrderDaoImpl extends BaseDaoImpl<Userordert> implements IUserOrderDao {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;
	
	/**
	 * @Description:获取订单状态
	 * @param orderId 订单id
	 * @author fengshuonan
	 * @return Integer 订单状态   1:待抢单  2:待支付  3:已支付  4:已完成  5:已取消
	 * @since 2.3.0
	 */
	@Override
	@Transactional(readOnly = true)
	public Integer getUserOrderStatus(String orderId) {
		
		if(ToolUtil.isBlank(orderId)){
			log.error("所查订单id为空！UserOrderDaoImpl:getUserOrderStatus()");
			return null;
		}
		
		try {
			Userordert userordert = super.getById(orderId);
			if(userordert != null){
				return userordert.getOrderStatus();
			}
		} catch (Exception e) {
			log.error("查询订单状态出错！UserOrderDaoImpl:getUserOrderStatus()",e);
		}
		
		return null;
	}

	/**
	 * @Description:通过用户id获取用户的手机号
	 * @param userId 用户id
	 * @author fengshuonan
	 * @return String 电话号码
	 * @since 2.4
	 */
	@Override
	public String getUserTelephone(String userId) {
		
		UserLogin userlogin = this.baseDaoX.getById(userId, UserLogin.class);
		
		if(userlogin == null){
			throw new BussinessException(BizExpEnum.NO_THIS_USER);
		}
		
		return userlogin.getUserName();
	}
	
}
